package com.kystar.muxplayer.ui;

import android.content.Context;
import android.graphics.Rect;
import android.inputmethodservice.Keyboard;

import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.model.Layer;
import com.kystar.muxplayer.selfview.MyKeyBoard;

/**
 * Created by zhusheng on 2018-03-10.
 */
public class LayerGroupView extends GroupView{
    private Layer layer;
    private OkCancelView viewButtons;
    private OkCancelView sourceButtons;
    private RectGroupView viewRect;
    private RectGroupView sourceRect;
    private ViewList localGlobal;
    private ViewList advanceButtons;
    private ColorSeekBar seekBarFilter;
    private ColorSeekBar seekBarFeather;
    private ColorSeekBar seekBarAlpha;
    MyKeyBoard mKeyboard;

    public LayerGroupView(Context context, MyKeyBoard myKeyBoard) {
        super(context, "图层设置");
        mKeyboard = myKeyBoard;
        addSubGroup("图层参数", R.layout.layout_main_settings_layer_view, false);
        addSubGroup("局部设置", R.layout.layout_main_settings_layer_source, false);
        addSubGroup("全局/局部", R.layout.layout_main_settings_layer_full, false);
        addSubGroup("高级设置", R.layout.layout_main_settings_layer_advance, true);
        bindView();
    }

    public void selectLayer(Layer layer) {
        this.layer = layer;
        updateView();
    }

    public void bindView() {
        viewRect = new RectGroupView(mContentView.findViewById(R.id.layer_view_rect),mKeyboard);
        sourceRect = new RectGroupView(mContentView.findViewById(R.id.layer_source_rect),mKeyboard);

        viewButtons = new OkCancelView(mContentView.findViewById(R.id.layer_view_buttons));
        sourceButtons = new OkCancelView(mContentView.findViewById(R.id.layer_source_buttons));

        // 图层参数
        viewButtons.watch(viewRect).onOk(()->{
            Rect rect = new Rect();
            viewRect.getRect(rect);
//            connection.run(p -> {
//                p.setLayerRect(layer.getPipId(), rect);
//                return p.getLayer(layer.pipType);
//            }).onData(data->{
//                muxModel.updateLayer(data, Event.EventSource.Settings);
//                viewButtons.cancel();
//            }).post();
        }).onCancel(() -> {
           // viewRect.setRect(layer.getViewRect());
        }).hide();

        //局部参数
        sourceButtons.watch(sourceRect).onOk(()->{
            Rect rect = new Rect();
            sourceRect.getRect(rect);
//            connection.run(p -> {
//                p.setLayerClip(layer.getPipId(), rect);
//                return p.getLayer(layer.pipType);
//            }).onData(data->{
//                muxModel.updateLayer(data, Event.EventSource.Settings);
//                sourceButtons.cancel();
//            }).post();
        }).onCancel(() -> {
           // sourceRect.setRect(layer.getSourceRect());
        }).hide();

        // 全局/局部
        localGlobal = new ViewList(findViewById(R.id.layer_local_global_buttons), true);
        localGlobal.setOnClickListener(v -> {
            boolean global = "global".equals(Helper.getTag(v));
//            connection.post(p -> {
//                return p.setLayerInfo(layer.getPipId(),
//                        global? LayerOperateType.Global : LayerOperateType.Local, -1);
//            });
            layer.setGlobal(global);
        });

        // 高级设置
        seekBarFilter = mContentView.findViewById(R.id.csb_advance_filter);
        seekBarFeather = mContentView.findViewById(R.id.csb_advance_feather);
        seekBarAlpha = mContentView.findViewById(R.id.csb_advance_alpha);
        advanceButtons = new ViewList(mContentView, R.id.layer_advance_buttons, true);
        advanceButtons.setOnClickListener(v -> {
            Helper.hideView(seekBarFilter);
            Helper.hideView(seekBarFeather);
            Helper.hideView(seekBarAlpha);//todo layer is
            switch (Helper.getTag(v)) {
                case "filter":
                    Helper.showView(seekBarFilter);
                 //   seekBarFilter.setValue(layer.getFilterColor());
                    break;
                case "feather":
                    Helper.showView(seekBarFeather);
               //     seekBarFeather.setValue(layer.getFeatherRadius());
                    break;
                case "alpha":
                    Helper.showView(seekBarAlpha);
                //    seekBarAlpha.setValue(layer.getAlpha());
                    break;
                default:
                   // Verify.fail("unknown tag");
            }
        });
        seekBarFilter.setMax(255);
        seekBarFeather.setMax(20);
        seekBarAlpha.setMax(100);
        seekBarFilter.setOnChangListener(value -> {
            if (value != layer.getFilterColor()) {
             //   connection.post(p -> p.setLayerFilter(layer.getPipId(), Protocol.LayerCmdType.FilterColor, value));
                layer.setFilterColor(value);
            }
        });
        seekBarFeather.setOnChangListener(value -> {
            if (value != layer.getFeatherRadius()) {
            //    connection.post(p -> p.setLayerFilter(layer.getPipId(), Protocol.LayerCmdType.Feather, value));
                layer.setFeatherRadius(value);
            }
        });
        seekBarAlpha.setOnChangListener(value -> {
            if (value != layer.getAlpha()) {
           //     connection.post(p -> p.setLayerFilter(layer.getPipId(), Protocol.LayerCmdType.Alpha, value));
                layer.setAlpha(value);
            }
        });
    }

    public void updateView() {
//        Helper.setText(mContentView, R.id.layer_view_name, layer.getPipName());
//        Helper.setText(mContentView, R.id.layer_source_name, layer.getPipName());

        viewButtons.cancel();
        sourceButtons.cancel();
        seekBarFilter.setValue(layer.getFilterColor());
        seekBarFeather.setValue(layer.getFeatherRadius());
        seekBarAlpha.setValue(layer.getAlpha());
        localGlobal.select(layer.isGlobal()?0:1, false);

        advanceButtons.select(0, true);
    }

    public void OnFocusChange(Layer from, Layer to) {
        if (to != null) {
            selectLayer(to);
        }
    }

//    public void OnChange(Layer pip, Event.EventSource eventSource, Event.EventType eventType) {
//        if (pip != this.layer || eventSource== Event.EventSource.Settings) {
//            return;
//        }
//        switch (eventType) {
//            case PipViewRect:
//                viewButtons.cancel();
//                break;
//            case PipSourceRect:
//                sourceButtons.cancel();
//                break;
//            default:
//                updateView();
//                break;
//        }
//    }
}
